package com.sunlg.smarthome.ui.personal;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.NotificationContract;
import com.sunlg.smarthome.contract.presenter.NotificationPresenter;
import com.sunlg.smarthome.data.entity.Notification;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NotificationActivity extends BaseActivity
        implements NotificationContract.View{

    @BindView(R.id.rv_notification_list)
    RecyclerView rvNotificationList;

    private NotificationContract.Presenter presenter;
    private NotificationAdapter notificationAdapter;
    private List<Notification> notificationList;
    private PreferHelper preferHelper;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_notification;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("通知");

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        presenter = new NotificationPresenter();
        presenter.attachView(this);

        preferHelper = App.getInstance().getPreferHelper();

        notificationList = new ArrayList<>();
        notificationAdapter = new NotificationAdapter(this, notificationList);
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvNotificationList.setLayoutManager(layoutManager);
        rvNotificationList.setAdapter(notificationAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.loadAllNotification();
    }

    @Override
    public void loadSuccess(List<Notification> notificationList) {
        this.notificationList.clear();
        this.notificationList.addAll(notificationList);
        notificationAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }
}
